package so.sao.code.service.domain.entity.tagbatch;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import so.sao.code.service.domain.entity.TagBatch;
import so.sao.code.service.infrastructure.dozer.Mapper;

/**
 * Created by Administrator on 2017/9/8.
 */

public class DefaultTagBatch extends TagBatch {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1373658430834811269L;

	public List<TagBatch> convertToList() {
		List<TagBatch> tagBatches = new ArrayList<>();
		long seqnum = getSeqnumStart();
		for (int i = 0; i < getUnitCount(); i++) {
			TagBatch tmpTagBatch = Mapper.map(this, TagBatch.class);
			tmpTagBatch.setTagCount(getCount());
			tmpTagBatch.setUnitCount(1);
			tmpTagBatch.setName(StringUtils.join(getName(), "-", i + 1));
			tmpTagBatch.setSeqnumStart(seqnum);
			seqnum += getCount();
			tmpTagBatch.setSeqnumEnd(seqnum - 1);
			tagBatches.add(tmpTagBatch);
		}
		replaceName(tagBatches);
		return tagBatches;
	}

	/**
	 * 只生成一批码时去除名字后的数字
	 */
	private void replaceName(List<TagBatch> list) {
		if (list.size() == 1) {
			String name = list.get(0).getName();
			list.get(0).setName(name.substring(0, name.lastIndexOf('-')));
		}
	}

	/**
	 * 将码数量分配到多个批次中
	 */
	private Long getCount() {
		Long value = getTagCount() / getUnitCount();
		return value;
	}
}
